package com.childenglish.controller;

import com.childenglish.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @RequiresPermissions("system:user:view")
    @GetMapping("/users")
    public R getUserManagement() {
        Map<String, Object> data = new HashMap<>();

        // 用户列表
        List<Map<String, Object>> users = Arrays.asList(
                createUser(1L, "admin", "管理员", "admin", "0", "2024-01-01"),
                createUser(2L, "parent_zhang", "张爸爸", "parent", "0", "2024-01-02"),
                createUser(3L, "child_xiaoming", "小明", "child", "0", "2024-01-02"),
                createUser(4L, "teacher_li", "李老师", "teacher", "0", "2024-01-03")
        );

        // 角色列表
        List<Map<String, Object>> roles = Arrays.asList(
                createRole(1L, "超级管理员", "admin", "拥有所有权限"),
                createRole(2L, "家长", "parent", "查看孩子学习进度"),
                createRole(3L, "幼儿", "child", "学习功能权限"),
                createRole(4L, "教师", "teacher", "教学管理权限")
        );

        // 权限列表
        List<Map<String, Object>> permissions = Arrays.asList(
                createPermission("system:user:view", "查看用户", "系统管理"),
                createPermission("system:user:edit", "编辑用户", "系统管理"),
                createPermission("system:user:delete", "删除用户", "系统管理"),
                createPermission("system:role:view", "查看角色", "系统管理"),
                createPermission("system:role:edit", "编辑角色", "系统管理"),
                createPermission("system:role:delete", "删除角色", "系统管理"),
                createPermission("system:permission:view", "查看权限", "系统管理"),
                createPermission("system:permission:edit", "编辑权限", "系统管理"),
                createPermission("learning:listening:view", "听力学习", "学习功能"),
                createPermission("learning:reading:view", "绘本阅读", "学习功能"),
                createPermission("learning:game:view", "游戏学习", "学习功能"),
                createPermission("parent:child:view", "查看孩子", "家长功能"),
                createPermission("parent:progress:view", "查看进度", "家长功能"),
                createPermission("parent:plan:edit", "编辑计划", "家长功能"),
                createPermission("teaching:class:view", "查看班级", "教师功能"),
                createPermission("teaching:content:edit", "编辑内容", "教师功能"),
                createPermission("teaching:report:view", "查看报告", "教师功能")
        );

        data.put("users", users);
        data.put("roles", roles);
        data.put("permissions", permissions);

        return R.ok("获取管理数据成功").data(data);
    }

    @RequiresPermissions("system:role:edit")
    @PostMapping("/role/permissions")
    public R updateRolePermissions(@RequestBody Map<String, Object> request) {
        Long roleId = Long.valueOf(request.get("roleId").toString());
        @SuppressWarnings("unchecked")
        List<String> permissions = (List<String>) request.get("permissions");

        // 这里应该调用服务更新数据库
        Map<String, Object> result = new HashMap<>();
        result.put("roleId", roleId);
        result.put("permissions", permissions);
        result.put("updateTime", new Date());

        return R.ok("权限更新成功").data(result);
    }

    @RequiresPermissions("system:user:edit")
    @PostMapping("/user/role")
    public R updateUserRole(@RequestBody Map<String, Object> request) {
        Long userId = Long.valueOf(request.get("userId").toString());
        Long roleId = Long.valueOf(request.get("roleId").toString());

        // 这里应该调用服务更新数据库
        Map<String, Object> result = new HashMap<>();
        result.put("userId", userId);
        result.put("roleId", roleId);
        result.put("updateTime", new Date());

        return R.ok("用户角色更新成功").data(result);
    }

    private Map<String, Object> createUser(Long id, String username, String nickname, String role, String status, String createTime) {
        Map<String, Object> user = new HashMap<>();
        user.put("id", id);
        user.put("username", username);
        user.put("nickname", nickname);
        user.put("role", role);
        user.put("status", status);
        user.put("createTime", createTime);
        return user;
    }

    private Map<String, Object> createRole(Long id, String name, String key, String description) {
        Map<String, Object> role = new HashMap<>();
        role.put("id", id);
        role.put("name", name);
        role.put("key", key);
        role.put("description", description);
        return role;
    }

    private Map<String, Object> createPermission(String code, String name, String category) {
        Map<String, Object> permission = new HashMap<>();
        permission.put("code", code);
        permission.put("name", name);
        permission.put("category", category);
        return permission;
    }
}